package com.hpin.fansea.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hpin.fansea.R;
import com.hpin.fansea.adapter.CommentAdapter;
import com.hpin.fansea.base.BaseActivity;
import com.hpin.fansea.bean.CommentList;
import com.hpin.fansea.bean.CommentResultBean;
import com.hpin.fansea.bean.GuideList;
import com.hpin.fansea.utils.CommonUtils;
import com.hpin.fansea.utils.Constant;
import com.hpin.fansea.utils.HttpUtil;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 办事指南详情页面
 * Created by Aaron on 15/9/9.
 */
public class GuideDetailActivity extends BaseActivity {

    private static final int GUIDE_COMMENT = 12;

    //评论按钮
    private TextView guide_detail_comment;
    //    private TextView guide_detail_date;
    private TextView guide_detail_title;
    private ImageView guide_detail_img;
    private TextView guide_detail_content;

    private GuideList guideList;
    private ListView guidw_commentList;

    private CommentAdapter commentAdapter = null;
    private List<CommentList> commentLists = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_guide_detail);

        commentLists = new ArrayList<CommentList>();
        guideList = (GuideList) getIntent().getExtras().getSerializable("guideList");

        initWidget();

        setData();

        //获取评论数据
        getCommentData();

    }

    /**
     * 获取评论数据
     */
    private void getCommentData() {

        HttpUtil.getInstance().setGetRequest("加载中...", this,
                Constant.WEB_ROOT + Constant.GUIDECOMMENTLIST + guideList.id_
                        + "?token=" + sp.getString(Constant.token, "") + "&pageSize="
                        + 1000 + "&pageIndex=" + 0
                        + "&systemType=" + Constant.SYSTEM_TYPE, new HttpUtil.SuccessListener() {
                    @Override
                    public void setOnSuccessListener(String str) {

                        try {

                            CommentResultBean commentList = JSONObject.parseObject(str, CommentResultBean.class);

                            if (Constant.STATUS_SUCESS.equals(commentList.status)) {

                                commentLists = commentList.result;
                                commentAdapter.setData(commentLists);
                                commentAdapter.notifyDataSetChanged();

                            }

                        } catch (Exception e) {
                            e.printStackTrace();
                            showToast("加载评论数据失败");
                        }

                    }
                }, null);

    }

    private void initWidget() {

        ImageView iv_left = (ImageView) findViewById(R.id.iv_left);
        TextView tv_title = (TextView) findViewById(R.id.tv_title);
        guide_detail_comment = (TextView) findViewById(R.id.tv_right);
        tv_title.setText(getResources().getString(R.string.guide_work));

        iv_left.setOnClickListener(this);
        guide_detail_comment.setOnClickListener(this);

        guide_detail_comment.setText(getResources().getString(R.string.comment));

        guidw_commentList = (ListView) findViewById(R.id.guidw_commentList);

        // 或得guide的view
        View my_head = getLayoutInflater().inflate(R.layout.guide_detail_head, null);
        commentAdapter = new CommentAdapter(this, commentLists);
        guidw_commentList.addHeaderView(my_head);
        guidw_commentList.setAdapter(commentAdapter);

//        guide_detail_date = (TextView) my_head.findViewById(R.id.guide_detail_date);
        guide_detail_title = (TextView) my_head.findViewById(R.id.guide_detail_title);
        guide_detail_img = (ImageView) my_head.findViewById(R.id.guide_detail_img);
        guide_detail_content = (TextView) my_head.findViewById(R.id.guide_detail_content);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == GUIDE_COMMENT) {

            //TODO
            if (data == null)
                return;

            CommentList comment = (CommentList) data.getSerializableExtra("comment");

            if (commentLists !=null) {

                commentLists.add(0, comment);
                commentAdapter.setData(commentLists);
                commentAdapter.notifyDataSetChanged();

            }

        }
    }

    private void setData() {

        guide_detail_title.setText(guideList.guideName);

        guide_detail_content.setText(guideList.guideContent);

//        if (!CommonUtils.isNull(guideList.createTime)) {//发布日期
//            guide_detail_date.setText(guideList.createTime);
//        } else {
//            guide_detail_date.setText("");
//        }

        if (CommonUtils.isNull(guideList.fmpic)) {
            guide_detail_img.setVisibility(View.GONE);
        } else {
            guide_detail_img.setVisibility(View.VISIBLE);
            ImageLoader.getInstance().displayImage(guideList.fmpic, guide_detail_img);
        }

    }

    @Override
    public void widgetClick(View v) {

        switch (v.getId()) {

            case R.id.iv_left:

                finish();

                break;
            case R.id.tv_right://评论

                if (!Constant.STATUS_VISITOR.equals(sp.getString(Constant.status, ""))) {
                    Intent intent = new Intent(this, ReleaseCommentActivity.class);
//                      Bundle bundle = new Bundle();
//                      if (guideList != null) {
//                          bundle.putSerializable("guideList", guideList);
//                      }
//                      intent.putExtras(bundle);
                    intent.putExtra("guiedId", guideList.id_);
                    startActivityForResult(intent, GUIDE_COMMENT);
                } else {
                    remindLogin("亲,登录后才能评论哦");
                }

                break;

        }

    }
}
